package com.heima.controller;


import com.heima.pojo.dto.PartnerQueryParam;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.domain.Region;
import com.heima.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/vm-service/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    /**
     * 分页查询
     * @param pqp
     * @return
     */
    @GetMapping("/search")
    public PageResult search(PartnerQueryParam pqp){
        PageResult pageResult = regionService.regionQueryPage(pqp);
        return pageResult;
    }

    /**
     * 新增区域
     */
    @PostMapping
    public ResponseEntity<Boolean> add( @RequestBody Region region){
        Boolean res = regionService.insertRegion(region);
        return ResponseEntity.ok(res);
    }

/*      @PostMapping
 public ResponseEntity<Boolean> add(HttpServletRequest request) {
        // 打印所有请求参数
        System.out.println("=== 请求参数列表 ===");
        request.getParameterNames().asIterator().forEachRemaining(paramName -> {
            System.out.println(paramName + " = " + request.getParameter(paramName));
        });

        // 获取参数值
        String name = request.getParameter("regionName");
        String remark = request.getParameter("remark");

        Boolean res = regionService.insertRegion(name, remark);
        return ResponseEntity.ok(res);
    }*/

    /**
     * 修改区域
     * @param regionName
     * @param remark
     * @return
     */
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> update(@RequestBody Region region,@PathVariable String id){
        Boolean res = regionService.updateRegion(region,id);
        return ResponseEntity.ok(res);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable String id){
        Boolean res = regionService.del(Long.valueOf(id));
        return ResponseEntity.ok(res);
    }

}
